package com.tuanzi.loan.web.bean.businesssystem;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.businesssystem.BusinessMain;
import com.tuanzi.loan.business.entity.businesssystem.BusinessProduct;
import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.service.businesssystem.BusinessMainService;
import com.tuanzi.loan.business.service.businesssystem.BusinessProductService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.businesssystem.BusinessIncomeRequest;
import com.tuanzi.loan.vo.response.businesssystem.BusinessIncomeResponse;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.loan.web.lazy.businesssystem.BusinessIncomeLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.model.LazyDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.Collections;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;


@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class BusinessProductInfoBean extends JSFInfoBean<BusinessProduct> {

    @Autowired
    private BusinessProductService businessProductService;
    @Autowired
    private BusinessMainService businessMainService;

    private List<ProductInfo> activityList;

    private List<Long> activityIdList;

    private List<BusinessMain> companyList;

    private BusinessIncomeRequest incomeRequest;

    private LazyDataModel<BusinessIncomeResponse> incomeResponseLazyDataModel;

    @PostConstruct
    private void init() {
        String id = getParamFromRequest("id");
        activityList = businessProductService.getActivityList();
        companyList = businessMainService.getCompanyList();
        incomeRequest = new BusinessIncomeRequest();
        if (StringUtils.isNotBlank(id)) {
            $ = businessProductService.findOne(Long.parseLong(id));
            //查询指定产品的消耗记录
            initIncomeLazyDataModel();

        } else {
            activityIdList = Lists.newArrayList();
            $ = new BusinessProduct();
            changeStatusToAdd();
        }
    }

    public void initIncomeLazyDataModel() {
        incomeRequest.setChannelId(Collections.singletonList($.getId().intValue()));
        incomeResponseLazyDataModel = new BusinessIncomeLazyDataModel(incomeRequest);
    }

    public void onReset() {
        incomeRequest = new BusinessIncomeRequest();
        initIncomeLazyDataModel();
    }
    public String onReturn(String url){
        return redirect(url,getParam());
    }

    public void onSave() {
        boolean validate = validate();
        if (validate == false) return ;

        $ = businessProductService.saveOrUpdate($);
        changeStatusToView();
        addSuccessfullyMessage();
    }
    public boolean validate() {
        if ($.getChannelId() == null) {
            addErrorMessage("没有渠道ID");
            return false;
        }
        if ($.getChannelName() == null) {
            addErrorMessage("没有渠道名称");
            return false;
        }
        if ($.getPayMode() == null) {
            addErrorMessage("没有结算方式");
            return false;
        }
        if ($.getPricePay() == null &&( $.getPayMode()==1 || $.getPayMode()==2  || $.getPayMode()==3 || $.getPayMode()==4)) {
            addErrorMessage("结算金额为空");
            return false;
        }
        if ($.getCompanyId()==null) {
            addErrorMessage("没有所属主体");
            return false;
        }
        return true;
    }

    public void onDelete(Long id) throws Exception {
        BusinessProduct one = businessProductService.findOne(id);
        businessProductService.delete(one);
        addSuccessfullyMessage();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(SETTLEMENT_CATEGORY, COOPERATION_MODE,HIT_LIBRARY,
                SETTLEMENT_CYCLE, SETTLEMENT_SPREAD_MODE, SETTLEMENT_SPREAD_CHANNEL, CHANNEL_TYPE, SETTLEMENT_CYCLE);
    }
}
